package com.zhang.warehouse.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.common.dto.OrderLockStockDto;
import com.zhang.common.exception.myexception.WareLockInsufficientException;
import com.zhang.warehouse.entity.WareInfo;
import com.zhang.warehouse.entity.WareSku;
import com.zhang.warehouse.mapper.WareSkuMapper;
import com.zhang.warehouse.service.IWareSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仓库保存商品的信息 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2023-10-13
 */
@Service
public class WareSkuServiceImpl extends ServiceImpl<WareSkuMapper, WareSku> implements IWareSkuService {
//   TODO 默认是一个sku只存在一个仓库中，如果后面有sku分为几个仓库存储，这里都需要进行更改

    @Override
    public IPage getWareSkuListPage(Integer pageNum, Integer pageSize, Map<String, Object> wareSkuSearchDto) {
        QueryWrapper<WareSku> wrapper = new QueryWrapper<>();
        String skuId = String.valueOf(wareSkuSearchDto.get("skuId"));
        if (!StrUtil.isEmpty(skuId)) {
            wrapper.eq("sku_id", skuId);
        }
        String wareId = String.valueOf(wareSkuSearchDto.get("wareId"));
        if (!StrUtil.isEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        Page<WareSku> page = this.page(new Page<>(pageNum, pageSize), wrapper);
        return page;
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer stock, String skuName) {
        List<WareSku> list = list(new QueryWrapper<WareSku>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (list == null || list.size() == 0) {
            WareSku wareSku = new WareSku();
            wareSku.setWareId(wareId);
            wareSku.setSkuName(skuName);
            wareSku.setSkuId(skuId);
            wareSku.setStock(stock);
            /*新增操作*/
            save(wareSku);
        } else {
            baseMapper.addStock(skuId, wareId, stock);
        }
    }

    @Override
    public Integer getStockBySkuId(Long skuId) {
        Integer stock = baseMapper.getStockBySkuId(skuId);
        return stock == null ? 0 : stock;
    }

    @Override
    public void orderLockStock(List<OrderLockStockDto> collect) {
        for (OrderLockStockDto lock : collect) {
            Integer stock = getStockBySkuId(lock.getSkuId());
            if (stock < lock.getCount()) {
//                如果库存不足，则抛出异常
                throw new WareLockInsufficientException(lock.getSkuId().toString());
            }
            WareSku wareSku = new WareSku();
            wareSku.setSkuId(lock.getSkuId());
            wareSku.setStockLocked(lock.getCount().toString());
            baseMapper.LockStock(wareSku);
        }
    }

    @Override
    public void deductStock(@Param("skuId") Long skuId, @Param("skuQuantity") Integer skuQuantity) {
        baseMapper.deductStock(skuId, skuQuantity);
    }

    @Override
    public void returnSkuStock(Long skuId, Integer skuQuantity) {
        baseMapper.returnSkuStock(skuId,skuQuantity);
    }
}
